package bean;

import string.UtilString;

import java.lang.reflect.Field;
import java.util.*;

/**
 * Description: domain infomation
 * Created by 4790K_NSL on 2017/5/4.
 */
public class UtilBeanInfo {

    //定义简单类名数组：
    //简单类包括:原始类、包装类、String，类名为小写，且为simpleName
    public static List<String> SIMPLE_CLASSES_List= Arrays.asList("boolean","byte","char","short",
            "int","long","float","double","String","Boolean",
            "Byte","Char","Short",
            "Int","Long","Float","Double","Integer");

    public static List<String> SIMPLE_CLASSES_List_LOWCASE= Arrays.asList("boolean","byte","char","short",
            "int","long","float","double","string", "integer");

    static public boolean isSmpleField(String fieldName){
        return SIMPLE_CLASSES_List_LOWCASE.contains(fieldName);
    }

    static public boolean isSmpleField(Field field){
        String fieldName=field.getGenericType().toString();
        fieldName =UtilString.getLastSubString(fieldName,".").toLowerCase();
        return SIMPLE_CLASSES_List_LOWCASE.contains(fieldName);
    }

    static public List<Field> getFiledList(Class clazz){
        Field[] fields=clazz.getDeclaredFields();
        List list=new ArrayList(fields.length);
        list.addAll(Arrays.asList(fields));
        return list;
    }


    /**
     * 获取本类所有属性名组成的 数组
     *
     * @param clazz
     * @return*/
    public static String[] getFiledNameArray(Class clazz){
        Field[] fields=clazz.getDeclaredFields();
        String[] fieldNames=new String[fields.length];
        for(int i=0;i<fields.length;i++){
            //System.out.println(fields[i].getType());
            fieldNames[i]=fields[i].getName();
        }
        return fieldNames;
    }

    /** 获取本类所有属性名组成的 List
     * @param clazz
     * @return
     */
    public static List<String> getFiledNameList(Class clazz){
        String[] fieldNames=getFiledNameArray(clazz);
        List<String> list=new ArrayList<>();
        list.addAll(Arrays.asList(fieldNames));
        return list;
    }

    /** 获取本类所有属性名组成的 Set
     * @param clazz
     * @return
     */
    public static Set<String> getFieldNameSet(Class clazz){
        Set<String> set=new HashSet<>();
        List list=getFiledNameList(clazz);
        set.addAll(list);
        return set;
    }

    /** 获取本类和他父类所有属性名组成的 List
     * @param clazz
     * @return
     */
    public static List<String> getFiledNameListIncludingParents(Class clazz){
        List<String> fieldNameListAll=new ArrayList<String>();
        return getFiledNameList(clazz,fieldNameListAll);
    }

    /** 获取本类和他父类所有属性名组成的 Set
     * @param clazz
     * @return
     */
    public static Set<String> getFieldNameSetIncludingParents(Class clazz){
        Set<String> set=new HashSet<>();
        set.addAll(getFiledNameListIncludingParents(clazz));
        return set;
    }

    /**
     * 获取属性名组成 List ,包括 父类中的属性名称
     *
     * @param selfClass
     * @param fieldNameListAll
     * @return*/
    public static List<String> getFiledNameList(Class selfClass,List<String> fieldNameListAll){
        Class parentClass=selfClass.getSuperclass();
        if(selfClass==Object.class){
            return fieldNameListAll;
        }
        Field[] fields=selfClass.getDeclaredFields();
        List fieldNameList=new ArrayList<>();
        for(int i=0;i<fields.length;i++){
            System.out.println(fields[i].getName());
            fieldNameList.add(fields[i].getName());

        }
        fieldNameListAll.addAll(fieldNameList);
        getFiledNameList(parentClass,fieldNameListAll);
        return fieldNameListAll;
    }

    static public Map<String,Field> getFieldNameMap(Class clazz){
       Field[] fields=clazz.getDeclaredFields();
       Map<String,Field> fieldNameMap=new HashMap<>();
        for (Field field : fields) {
            fieldNameMap.put(field.getName(),field);
        }
        return  fieldNameMap;
    }

    static public Map<String,Field> getFieldNameMapOfLowCase(Class clazz){
        Field[] fields=clazz.getDeclaredFields();
        Map<String,Field> fieldNameMap=new HashMap<>();
        for (Field field : fields) {
            fieldNameMap.put(field.getName().toLowerCase(),field);
        }
        return  fieldNameMap;
    }
}
